import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/mine/user_manage/user_manager_user_detail/tree_expand.dart';
import 'package:yt_devops/pages/mine/user_manage/user_manager_user_detail/tree_top_expansion_card.dart';
import 'package:yt_devops/styles/styles.dart';

import '../../../../components/yt_expandable_tree/model/tree_mode.dart';

///
/// @Description TODO
/// @Author liuer
/// @CreateTime 2023年12月19日 11:14
class ChooseOrganizationTree extends StatefulWidget {
  final List<TreeNode> treeData;

  const ChooseOrganizationTree(this.treeData, {super.key});

  @override
  State<ChooseOrganizationTree> createState() => _ChooseOrganizationTreeState();
}

class _ChooseOrganizationTreeState extends State<ChooseOrganizationTree> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _buildWidgets();
  }

  _buildWidgets() {
    List<Widget> list = [];
    if (widget.treeData.isNotEmpty) {
      for (var element in widget.treeData) {
        var childTree = element.subNodes;
        List<Widget> subList = [];
        if (childTree != null && childTree!.isNotEmpty) {
          for (var subElement in childTree) {
            var subChildTree = subElement.subNodes;
            List<Widget> listContentWidget = [];
            if (subChildTree != null && subChildTree.isNotEmpty) {
              for (var contentWidget in subChildTree) {
                var text = Container(
                  padding: const EdgeInsets.only(left: 60),
                    color: Colors.white,child:
                Row(
                  children: [
                    Gaps.hGap20,
                    Text(contentWidget.title),
                    Spacer(),
                    Container(
                      padding: const EdgeInsets.only(right: 14),
                      child: IconButton(
                        iconSize: 20,
                        icon:(contentWidget.checked == 1 ?true:false)
                            ? const Icon(
                          Icons.check_circle,
                          color: Colour.primaryColor,
                        )
                            : const Icon(
                          Icons.circle_outlined,
                          color: Colour.diabledButtoBgColor,
                        ),
                        onPressed: () {
                            _cancelAllSelected();
                            contentWidget.checked= 0;
                            // if(contentWidget.checked == 1){
                            //   contentWidget.checked = 0;
                              setState(() {
                              });
                            //   return;
                            // }else{
                            //   contentWidget.checked = 1;
                            // }
                            Log.d('checked==' + contentWidget.checked.toString());

                        },
                      ),
                    ),
                  ],
                )
                );
                listContentWidget.add(text);
              }
            }
            Widget subWidget = TreeExpansionCard(
                title: subElement.title,
                isSelected: (subElement.checked ==1?true:false),
                contentWidget: listContentWidget,callBack: (bool selected){
                  _cancelAllSelected();
              for (var data in widget.treeData) {
                // data.checked = 0;
                var childTree = data.subNodes;
                  childTree?.forEach((childData) {
                    Log.d('subElement.title ==' + subElement.title .toString());
                    Log.d('childData.title ==' + childData.title .toString());
                    if(subElement.title == childData.title){
                      childData.checked = selected?1:0;
                      var  subChild = childData.subNodes;
                      subChild?.forEach((subChildData) {
                        subChildData.checked = selected?1:0;
                      });
                    }



                  });

              }
              setState(() {

              });
            },);
            subList.add(subWidget);
          }
        }
        var expansion = TreeTopExpansionCard(title: element.title, contentWidget: subList,
          marginLeft: 0, isSelected: element.checked==1?true:false,callBack: (bool selected){//选中 或者取消选中下面的子树
            element.checked = selected?1:0;
          for (var data in widget.treeData) {
                // data.checked = 0;
                var childTree = data.subNodes;
                if(element.title == data.title){
                  childTree?.forEach((childData) {
                    childData.checked = selected?1:0;
                  var  subChild = childData.subNodes;
                    subChild?.forEach((subChildData) {
                      subChildData.checked = selected?1:0;
                    });

                  });
                }else{
                  data.checked = 0;
                  childTree?.forEach((childData) {
                    childData.checked = 0;
                    var  subChild = childData.subNodes;
                    subChild?.forEach((subChildData) {
                      subChildData.checked = 0;
                    });
                  });
                }

              }
            setState(() {

            });
          },);

        list.add(expansion);
      }
    }
    return list;
  }

  //取消所有选中
  _cancelAllSelected(){
    for (var data in widget.treeData) {
      data.checked = 0;
      var childTree = data.subNodes;
      childTree?.forEach((childData) {
        childData.checked = 0;
        var  subChild = childData.subNodes;
        subChild?.forEach((subChildData) {
          subChildData.checked = 0;
        });

      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
        children: _buildWidgets());
  }
}
